package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.translator.util.LanCodeUtil;

import java.io.Serializable;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LanguageAdapter;
import flc.ast.databinding.ActivitySelLanguageBinding;
import flc.ast.utils.Content;
import stark.common.other.LanCode;

public class SelLanguageActivity extends BaseAc<ActivitySelLanguageBinding> {

    public static int type = 0;
    public static LanCode mCurSelLanCodeFrom, mCurSelLanCodeTo;

    private LanguageAdapter generalLanAdapter, allLanAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_language;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelLanguageBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvFrom.setOnClickListener(this);
        mDataBinding.tvTo.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        mDataBinding.rvGeneralLan.setLayoutManager(layoutManager);
        generalLanAdapter = new LanguageAdapter();
        generalLanAdapter.setOnItemClickListener(this);
        generalLanAdapter.setNewInstance(LanCodeUtil.getGeneralLanCodes());
        generalLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
        mDataBinding.rvGeneralLan.setAdapter(generalLanAdapter);

        //rvAllLan
        layoutManager = new LinearLayoutManager(mContext);
        mDataBinding.rvAllLan.setLayoutManager(layoutManager);
        allLanAdapter = new LanguageAdapter();
        allLanAdapter.setOnItemClickListener(this);
        allLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
        allLanAdapter.setNewInstance(LanCodeUtil.getAllLanCodes());
        mDataBinding.rvAllLan.setAdapter(allLanAdapter);

        mDataBinding.tvFrom.setText(mCurSelLanCodeFrom.getName());
        mDataBinding.tvTo.setText(mCurSelLanCodeTo.getName());
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvFrom:
                clearSelection();
                mDataBinding.tvFrom.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvFrom.setBackgroundResource(R.drawable.shape_language_sel);
                type = 0;
                generalLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
                allLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
                break;
            case R.id.tvTo:
                clearSelection();
                mDataBinding.tvTo.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvTo.setBackgroundResource(R.drawable.shape_language_sel);
                type = 1;
                generalLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
                allLanAdapter.setSelectedLanCode(type == 0 ? mCurSelLanCodeFrom : mCurSelLanCodeTo);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == generalLanAdapter) {
            LanCode selLanCode = generalLanAdapter.getItem(position);
            generalLanAdapter.setSelectedLanCode(selLanCode);
            generalLanAdapter.notifyDataSetChanged();
            Intent intent = new Intent();
            intent.putExtra(Content.LAN_CODE, selLanCode);
            setResult(RESULT_OK, intent);
            finish();
            //                selLanCodeAndFinish(selLanCode);
        } else if (adapter == allLanAdapter) {
            LanCode lanCode = allLanAdapter.getItem(position);
            LanCodeUtil.addLanCode2GeneralHead(lanCode);
            LanCodeUtil.saveGeneralLanCode2File();
            allLanAdapter.setSelectedLanCode(lanCode);
            allLanAdapter.notifyDataSetChanged();
            Intent intent = new Intent();
            intent.putExtra(Content.LAN_CODE, lanCode);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    protected void initData() {
        if (type==0){
            mDataBinding.tvFrom.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFrom.setBackgroundResource(R.drawable.shape_language_sel);
        }else {
            mDataBinding.tvTo.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvTo.setBackgroundResource(R.drawable.shape_language_sel);
        }
    }

    private void clearSelection() {
        mDataBinding.tvFrom.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvFrom.setBackgroundResource(0);
        mDataBinding.tvTo.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvTo.setBackgroundResource(0);
    }

}